package com.example.job;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.deng.template.common.unit.DateUtils;
import com.example.job.activity.BaseCompatActivity;
import com.example.job.activity.CheckActivity;
import com.example.job.activity.GptWebViewActivity;
import com.example.job.databinding.ActivityMainBinding;
import com.example.job.service.FloatingServer;

import java.util.Date;

public class MainActivity extends BaseCompatActivity {

    private ActivityMainBinding binding;

    private static boolean RUN_ING_FLAG = false;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        binding.startJob.setOnClickListener(startJob(this));
        SharedPreferences sharedPreferences = getSharedPreferences("deng-check", MODE_PRIVATE);
        String time = sharedPreferences.getString("expiresTime", "1990-01-01 00:00:00");
        binding.timeText.setText("VIP结束时间：" + time);
        Date endTime = DateUtils.stringToDate(time, DateUtils.DAY_TIME_FORMAT);
        new CountDownTimer(endTime.getTime() - new Date().getTime(), 1000) {
            public void onTick(long millisUntilFinished) {
                Date nowTime = new Date();
                long excess = endTime.getTime() - nowTime.getTime();
                long day = excess / (24 * 60 * 60 * 1000);
                long hour = (excess - day*(24 * 60 * 60 * 1000)) / (1000 * 60 * 60);
                long min = (excess - day*(24 * 60 * 60 * 1000) - hour * (1000 * 60 * 60)) / (1000 * 60);
                long m = (excess - day*(24 * 60 * 60 * 1000) - hour * (1000 * 60 * 60) - min * (1000 * 60)) / 1000;
                binding.daojishi.setText("VIP剩余时间: "
                        + day + "天"
                        + hour + "时"
                        + min + "分"
                        + m + "秒");
            }
            public void onFinish() {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), CheckActivity.class);
                startActivity(intent);
            }
        }.start();
        binding.toGptWebView.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), GptWebViewActivity.class);
            startActivity(intent);
        });
    }

    /**
     * 开始挂机按钮点击回调
     * @param activity 动作
     * @return null
     */
    public View.OnClickListener startJob(BaseCompatActivity activity) {
        return v -> {
            Log.i(getLogTag(), "-------------------startJob-------------------");
            if(RUN_ING_FLAG){
                Toast.makeText(this, "脚本正在运行，请勿重复开始", Toast.LENGTH_SHORT).show();
                Log.w(getLogTag(), "MainActivity-脚本正在运行，请勿重复开始");
                return;
            }
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "当前无权限，请授权", Toast.LENGTH_SHORT).show();
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 1);
                return;
            }
            else {
                RUN_ING_FLAG = true;
                Toast.makeText(this, "悬浮窗开启成功,后续脚本开发ing,敬请期待", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingServer.class));
            }
            moveTaskToBack(true);
        };
    }

    /**
     * activity结果处理
     * @param requestCode 请求码
     * @param resultCode 结果code
     * @param data 数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
            } else {
                RUN_ING_FLAG = true;
                Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingServer.class));
            }
        }
    }

}